package com.hkts.naturalgas.flow.service.serviceimpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.flow.entity.FlowInstanceinfo;
import com.hkts.naturalgas.flow.entity.FlowTemplateinfoCopy;
import com.hkts.naturalgas.flow.entity.FlowUserinfo;
import com.hkts.naturalgas.flow.mapper.FlowTemplateinfoCopyMapper;
import com.hkts.naturalgas.flow.mapper.FlowUserinfoMapper;
import com.hkts.naturalgas.flow.service.IFlowTemplateinfoCopyService;
import com.hkts.naturalgas.flow.service.IFlowUserinfoService;
import com.hkts.naturalgas.organizational.mapper.HontUserMapper;
import com.hkts.naturalgas.organizational.orgVo.HontUserRolelevelQueryVo;
import com.hkts.naturalgas.organizational.orgVo.HontUserRolelevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zy
 * @since 2022-09-21
 */
@Service
@Transactional
public class FlowTemplateinfoCopyServiceImpl extends ServiceImpl<FlowTemplateinfoCopyMapper, FlowTemplateinfoCopy> implements IFlowTemplateinfoCopyService {
   @Autowired
   private FlowTemplateinfoCopyMapper flowTemplateinfoCopyMapper;
   @Autowired
   private FlowUserinfoMapper flowUserinfoMapper;
    @Autowired
    private HontUserMapper hontUserMapper;
    @Override
    public void addFlowTemplateinfoCopy(FlowInstanceinfo flowInstanceinfo) {
        List<FlowTemplateinfoCopy> flowTemplateinfoCopies=new ArrayList<>();

        QueryWrapper<FlowUserinfo> query = Wrappers.query();
        query.lambda().eq(FlowUserinfo::getDataType,FlowUserinfo.DATA_TYPE_COPY)
                .eq(FlowUserinfo::getTemplateId,flowInstanceinfo.getTemplateId())
        .eq(FlowUserinfo::getTemplateVersion,flowInstanceinfo.getTemplateVersion());
        List<FlowUserinfo> flowUserinfoList=flowUserinfoMapper.selectList(query);
        List<String> flowUserinfosUser=flowUserinfoList.stream().filter(t->FlowUserinfo.USER_TYPE_USER.equals(t.getUserType())).map(FlowUserinfo::getUserRoleId).collect(toList());
        if(flowUserinfosUser.size()>0){
            for (String item:flowUserinfosUser
                 ) {
                FlowTemplateinfoCopy flowTemplateinfoCopy=new FlowTemplateinfoCopy();
                flowTemplateinfoCopy.setFlowInstanceId(flowInstanceinfo.getId());
                flowTemplateinfoCopy.setHontUserId(item);
                flowTemplateinfoCopies.add(flowTemplateinfoCopy);
            }
        }

        List<String> flowUserinfosRole=flowUserinfoList.stream().filter(t->FlowUserinfo.USER_TYPE_ROLE.equals(t.getUserType())).map(FlowUserinfo::getUserRoleId).collect(toList());
        if(flowUserinfosRole.size()>0){

            HontUserRolelevelQueryVo hontUserRolelevelQueryVo=new HontUserRolelevelQueryVo();
            hontUserRolelevelQueryVo.setRoleIds(flowUserinfosRole);
            List<HontUserRolelevelVo> hontUserRolelevelVoList= hontUserMapper.getHontUserAndRoleAndLevel(hontUserRolelevelQueryVo);
            if(hontUserRolelevelVoList.size()>0){
                for (HontUserRolelevelVo item:hontUserRolelevelVoList){
                    FlowTemplateinfoCopy flowTemplateinfoCopy=new FlowTemplateinfoCopy();
                    flowTemplateinfoCopy.setFlowInstanceId(flowInstanceinfo.getId());
                    flowTemplateinfoCopy.setHontUserId(item.getId());
                    flowTemplateinfoCopies.add(flowTemplateinfoCopy);
                }
            }
        }

        flowTemplateinfoCopyMapper.insertBatch(flowTemplateinfoCopies);

    }
}
